package com.graduation.rescuematerial.controller.system;

import com.graduation.rescuematerial.service.ApplyMaterialService;
import com.graduation.rescuematerial.service.CompanyService;
import com.graduation.rescuematerial.service.MaterialService;
import com.graduation.rescuematerial.service.UserService;
import com.graduation.rescuematerial.vo.RespBean;
import com.graduation.rescuematerial.vo.RespBeanEnum;
import com.graduation.rescuematerial.vo.StatisticsVo;
import com.graduation.rescuematerial.vo.UserVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author:CaiShuangLian
 * @FileName:
 * @Date:Created in  2022/4/29 20:06
 * @Version:
 * @Description:TODO
 */
@RestController
@RequestMapping("/sys/statistics")
@Api(tags = "系统管理员——数据统计")
public class SysStatisticsController {

    @Autowired
    private CompanyService companyService;
    @Autowired
    private UserService userService;
    @Autowired
    private MaterialService materialService;
    @Autowired
    private ApplyMaterialService applyMaterialService;

//    物资种类数量的可视化统计，近7/14/30天各物资种类的交易数量，各部门人数占比的柱状图。

    @ApiOperation("数据统计：X轴-》种类名称，Y轴-》对应的数量")
    @ResponseBody
    @PostMapping("/material")
    public RespBean getMaterialStatistics(@RequestBody StatisticsVo statisticsVo, HttpServletRequest request, HttpServletResponse response){
        String ticket=statisticsVo.getTicket();
        if(ticket!=null && !ticket.isEmpty()){
            //根据cookie获取用户信息
            UserVo userVo=userService.getUserByCookie(ticket,request,response);
            if(userVo!=null && userVo.getAction().equals("system_administrator")){
                return materialService.getMaterialStatistics(statisticsVo.getCompanyName());
            }
            return RespBean.error(RespBeanEnum.NO_OPERATION_PERMISSION,"非系统管理员，无法查看");
        }
        return RespBean.error(RespBeanEnum.ILLEGAL_OPERATION);
    }

    @ApiOperation("数据统计：X轴-》种类名称，Y轴-》对应的交易量")
    @ResponseBody
    @PostMapping("/order")
    public RespBean getOrderStatistics(@RequestBody StatisticsVo statisticsVo, HttpServletRequest request, HttpServletResponse response){
        String ticket=statisticsVo.getTicket();
        if(ticket!=null && !ticket.isEmpty()){
            //根据cookie获取用户信息
            UserVo userVo=userService.getUserByCookie(ticket,request,response);
            if(userVo!=null && userVo.getAction().equals("system_administrator")){
                return applyMaterialService.getOrderStatistics(statisticsVo.getCompanyName());
            }
            return RespBean.error(RespBeanEnum.NO_OPERATION_PERMISSION,"非系统管理员，无法查看");
        }
        return RespBean.error(RespBeanEnum.ILLEGAL_OPERATION);
    }

    @ApiOperation("数据统计：X轴-》部门名称，Y轴-》对应的部门人员数量")
    @ResponseBody
    @PostMapping("/user")
    public RespBean getUserStatistics(@RequestBody String ticket, HttpServletRequest request, HttpServletResponse response){
//        String ticket=statisticsVo.getTicket();
        if(ticket!=null && !ticket.isEmpty()){
            //根据cookie获取用户信息
            UserVo userVo=userService.getUserByCookie(ticket,request,response);
            if(userVo!=null && userVo.getAction().equals("system_administrator")){
                return companyService.getUserStatistics();
            }
            return RespBean.error(RespBeanEnum.NO_OPERATION_PERMISSION,"非系统管理员，无法查看");
        }
        return RespBean.error(RespBeanEnum.ILLEGAL_OPERATION);
    }
}
